package com.wangyadong.hobby.schedule.jobtask.javasync.cyclicbarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2019/1/719:44
 **/
public class CyclicBarrierExample2 {

    public static void main(String[] args) throws InterruptedException {
        final CyclicBarrier cyclicBarrier = new CyclicBarrier(2);

        new Thread() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(5);
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        //
        new Thread() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(7);
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        //
//        new Thread() {
//            @Override
//            public void run() {
//                try {
//                    cyclicBarrier.await();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                } catch (BrokenBarrierException e) {
//                    e.printStackTrace();
//                }
//            }
//        }.start();
        /////////////////////////
        TimeUnit.SECONDS.sleep(2);
        System.out.println(cyclicBarrier.getParties());
        System.out.println(cyclicBarrier.getNumberWaiting());
        System.out.println(cyclicBarrier.isBroken());
        TimeUnit.SECONDS.sleep(2);
        //打断其他线程
        cyclicBarrier.reset();
        System.out.println("=====================");
        System.out.println(cyclicBarrier.getParties());
        System.out.println(cyclicBarrier.getNumberWaiting());
        System.out.println(cyclicBarrier.isBroken());
    }
}
